Ext.define('Emp.view.role.UserConfig', {
	extend: 'Ext.window.Window',
	alias: 'widget.userconfig',

	title: '组用户分配',
	maximizable: true,
	modal: true,
	constrain: true,
	width: 400,
	height: 300,
	layout: 'fit',
	
	initComponent: function() {
		
		var me = this,
			roleId = me.roleId;
		if(!Ext.isDefined(roleId)) {
			me.close();
		}
		
		me.tree = Ext.create('Ext.tree.Panel', {
			border: false,
			hideHeaders: true,
			useArrows: true,
			rootVisible: false,
			store: Ext.create('Ext.data.TreeStore', {
				autoLoad: true,
				proxy: {
					type: 'ajax',
					url: PATH + '/role/queryuserroletree/' + roleId
				},
				listeners: {
					scope: me,
					'beforeload': function() {
	        			this.setLoading(true);
	        		},
					'load': function(_store, _records, _success) {
	        			this.setLoading(false);
	        		}
				}
			})
		});
		
		Ext.apply(me, {
			tools: [{
				type: 'refresh',
				handler: function(event, toolEl, panel) {
					me.tree.store.load();
				}
			}],
			items: [me.tree],
			buttons: [{
				text: '保&nbsp;&nbsp;存',
				iconCls: 'save',
				action: 'save',
				handler: function() {
					var datas = [],
						records = me.tree.getChecked();
					Ext.Array.each(records, function(item, index, allItems) {
						datas.push(item.internalId);
					});
					var	opts = {
						method: 'post',
						url: PATH + '/role/saveuserrole',
						params: {
							ids: datas,
							id: me.roleId
						},
						success: function(response, options) {
							var result = Ext.decode(response.responseText);
							if (result.success) {
								Ext.show.msg('提示', '保存成功');
							} else {
								Ext.Msg.show({
									title: '提示',
									msg: '保存失败!<br/>' + result.message,
									buttons: Ext.Msg.OK,
									icon: Ext.Msg.ERROR
								});
							}
						},
						failure: function(response, options) {
							Emp.msg.ajaxFailure(response, options)
						}
					};
					Ext.Ajax.request(opts);
				}
			}, {
				text: '关&nbsp;&nbsp;闭',
				iconCls: 'close',
				handler: function() {
					me.close();
				}
			}]
		});
		me.callParent();
	}
});